WARNING: To use this feature, your code will have to provide login information for an email provider. Be careful with this information -- hard-coding a username and password into a notebook that you might share is a good way to leak private information. You may wish to have your program read username and password information from another source (such as a file or the command line). Even better, you can create a separate email account for automated notifications, so that your program never has access to your "real" credentials.
(Please don't use the lab email account. If you accidentally release that information, it will be a pain for all of us to deal with.)
The Utilities package includes a wrapper for sending automated email alerts when a piece of code is done running. To do this, it will need to log into an email account, for which you must provide a username and password. It will also need to know where you want your alerts sent. This is done with the set_credentials
and set_destionation
functions. You only need to run these functions once per script.
Setting up an automatic message looks like this:
In [1]:
import murraylab_tools.utilities as mt_utils
import time
mt_utils.set_credentials("put_your_username_here", "put_your_password_here")
mt_utils.set_destination("sclamons@gmail.com")
with mt_utils.notify_when_done("A block"):
print("I'm doing a thing.")
print("I'm doing a second thing.")
time.sleep(3)
print("I'm done.")
The login above will fail, since I'm not providing real credentials here, but you can see how the program behaves when a login fails -- it will notify you that it didn't connect, and will keep running. If you want to see more information about how the connection went wrong, you can set that with the debug_connection flag:
In [2]:
with mt_utils.notify_when_done("A block", debug_connection = True):
print("I'm doing a thing.")
print("I'm doing a second thing.")
time.sleep(3)
print("I'm done.")
By default, the notify_when_done
class sends notifications to gmail's public smtp server at port 587. If you use another email provider, or if you want to use a different port for some reason, you can change the server and port in the call to notify_when_done
:
In [3]:
with mt_utils.notify_when_done("A block", server = "my.email.server", port = "777"):
print("I'm doing a thing.")
print("I'm doing a second thing.")
time.sleep(3)
print("I'm done.")
If you'd rather receive a text than an email, you can probably do so by setting your destination correctly. Most phone providers have an email service that will convert emails sent to some address into a text, using the following format:
So, for example, if your phone number is 555-432-1098 and you get your phone service through Republic Wireless, you would set your code to send you a text by running:
In [4]:
mt_utils.set_destination("5554321098@text.republicwireless.com")